//
//  ECListArrayOfHistory.m
//  Edu_iOS2018
//
//  Created by apple on 2018/2/6.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECListArrayOfHistory.h"

@implementation ECListArrayOfHistory

+(NSMutableArray *)getListArrayOfHistory{
    //获取本地搜索历史数组
    NSMutableArray *listArray = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"ECLastSearchArray"]];
    return listArray;
}

+(void)addListArrayOfHistoryWith:(NSString *)searchWorld{
    NSMutableArray *listArray = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"ECLastSearchArray"]];
    if (listArray.count > 0) {
        for (int i = 0; i < listArray.count; i++) {
            if ([searchWorld isEqualToString:listArray[i]]) {
                [listArray removeObjectAtIndex:i];//删除本地对应位置数据
                [listArray insertObject:searchWorld atIndex:0];//第一个位置新插入搜索值
                break;
            }else{
                if (i == listArray.count - 1) {
                    [listArray insertObject:searchWorld atIndex:0];
                    if (listArray.count > 10) {//最大值为10
                        [listArray removeLastObject];
                    }
                    break;
                }
            }
        }
    }else{//本地存储数组为空时
        [listArray addObject:searchWorld];
    }
    
    //存入数组并同步(在页面消失的时候进行存储)
    [[NSUserDefaults standardUserDefaults] setObject:listArray forKey:@"ECLastSearchArray"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(void)clearListArrayOfHistory{
    //获取本地搜索历史数组
    NSMutableArray *listArray = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"ECLastSearchArray"]];
    [listArray removeAllObjects];
    
    //存入数组并同步(在页面消失的时候进行存储)
    [[NSUserDefaults standardUserDefaults] setObject:listArray forKey:@"ECLastSearchArray"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(void)deleteListArrayOfHistoryOfIndex:(NSInteger)index{
    //获取本地搜索历史数组
    NSMutableArray *listArray = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"ECLastSearchArray"]];
    [listArray removeObjectAtIndex:index];
    
    //存入数组并同步(在页面消失的时候进行存储)
    [[NSUserDefaults standardUserDefaults] setObject:listArray forKey:@"ECLastSearchArray"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
